<template>
  <BKModal
    v-model="visible"
    width="700px"
    height="350px"
    @close="$emit('close')"
    :title="`${isEdit ? '编辑页面' : '新增页面'}`"
  >
    <el-container>
      <el-main style="padding: 20px 30px 0 10px !important;">
        <BKForm
          ref="form"
          size="mini"
          label-width="100px"
          :rules="rules"
          :items="items"
          :has-reset="false"
          :has-submit="false"
        />
      </el-main>
      <el-footer
        style="height: auto;"
        class="footer_box"
      >
        <div class="p-a-mini ta-c b-t">
          <el-button
            type="primary"
            @click="save"
            size="mini"
            :loading="saveLoading"
          >
            保存
          </el-button>
          <el-button
            size="mini"
            @click="$emit('close')"
            class="modal-form-close-button"
          >
            关闭
          </el-button>
        </div>
      </el-footer>
    </el-container>
  </BKModal>
</template>
<script>
import { mapState } from 'vuex'
const appId = 'YSJC'
const bzCode = 'YSJC'
export default {
  props: {
    formData: {
      type: Object,
      default: () => {}
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    visible: {
      type: Boolean,
      default: false
    },
    isCopy: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  data () {
    return {
      saveLoading: false,
      items: [
        { id: 2, type: 'input', label: '名称', field: 'PAGENM' },
        { id: 4, type: 'radio', label: '下级是否可见', field: 'SUBUN_SHOW', props: { dictionaryKey: 'DIC_GZT_SFKJ' } },
        { id: 7, type: 'textarea', label: '说明', field: 'PAGE_DESC', props: { rows: 5 } }
      ],
      rules: {
        PAGENM: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      }
    }
  },
  async created () {
    this.$nextTick(() => {
      this.$refs.form.setFormData(this.formData)
    })
  },
  mounted () {
  },
  methods: {
    async save () {
      try {
        this.saveLoading = true
        const valid = await this.$refs.form.validate()
        if (valid) {
          const formData = this.$refs.form.getFormData()
          if (!this.isEdit) {
            formData.CREATE_TIME = this.$utils.createDate(new Date()).format('YYYY-MM-DD HH:mm:ss')
          } else {
            delete formData.PAGE_CFG
          }
          await this.$request.post(this.isCopy ? '/ysjc/zhzs/copyPage' : '/ysjc/zhzs/savePage', {
            appId,
            bzCode,
            metaSet: 'YSQJC_ZHZS_PAGE',
            methodCode: this.isEdit ? 'update' : 'insert',
            methodParams: JSON.stringify(formData)
          })
          this.saveLoading = false
          this.$message.success('保存成功')
          // this.$emit('refresh')
          this.$emit('close', '1')
        } else {
          this.saveLoading = false
        }
      } catch (error) {
        this.saveLoading = false
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.self-btn {
  position: absolute;
  bottom: 15px;
}

::v-deep {
  .hide-unit .unit-contents {
    display: none;
  }
}

</style>
